﻿using Connection.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Connection
{
    class ConversationDB
    {
        private string cmdtext;
        private ConnectDB connectDB = new ConnectDB();
        private UserDB userDB = new UserDB();
        private MySqlConnection connection = null;

        #region Insert
        public int InsertConversation(string ConversationId, string TimeMess, string FromUserId, string Message)
        {            
            cmdtext = "INSERT INTO dbchat.conversation VALUES('" + ConversationId + "','" + TimeMess + "','" + FromUserId + "','" + Message + "')";
            return connectDB.QueryData(cmdtext);
        }
        #endregion

        #region Select
        public List<ConversationModel> SelectAllConversation()
        {
            //connectDB.setConnectionString(ref connectionString);
            connection = new MySqlConnection(connectDB.getConnectionString());
            cmdtext = "SELECT * FROM dbchat.conversation";
            MySqlCommand cmd = new MySqlCommand(cmdtext, connection);
            DataSet ds = new DataSet();
            List<ConversationModel> listConversationModel = new List<ConversationModel>();

            try
            {
                //mo ket noi
                connection.Open();
                MySqlDataAdapter adap = new MySqlDataAdapter();
                adap.SelectCommand = cmd;
                adap.Fill(ds);

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ConversationModel temp = new ConversationModel();
                    temp.ConversationId = ds.Tables[0].Rows[i][0].ToString();
                    temp.TimeMess = DateTime.Parse(ds.Tables[0].Rows[i][1].ToString());
                    temp.FromUserId = ds.Tables[0].Rows[i][2].ToString();
                    temp.Message = ds.Tables[0].Rows[i][3].ToString();

                    listConversationModel.Add(temp);
                }
                connection.Close();
                return listConversationModel;
            }
            catch
            {
                return null;
            }
            finally
            {
                //dong ket noi 
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }

        public List<string> SelectAllConversationDisplay(string ConversationID)
        {
            //connectDB.setConnectionString(ref connectionString); 
            connection = new MySqlConnection(connectDB.getConnectionString());
            cmdtext = "SELECT * FROM dbchat.conversation WHERE ConersationId = '" + ConversationID +"'";
            MySqlCommand cmd = new MySqlCommand(cmdtext, connection);
            DataSet ds = new DataSet();
            List<string> listConversation = new List<string>();

            try
            {
                //mo ket noi
                connection.Open();
                MySqlDataAdapter adap = new MySqlDataAdapter();
                adap.SelectCommand = cmd;
                adap.Fill(ds);

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    //ConversationModel tempConversation = new ConversationModel();
                    string tempString = userDB.SelectUsername(ds.Tables[0].Rows[i][2].ToString()) + ": " + ds.Tables[0].Rows[i][3].ToString();

                    listConversation.Add(tempString);
                }
                connection.Close();
                return listConversation;
            }
            catch
            {
                return null;
            }
            finally
            {
                //dong ket noi 
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
        #endregion
    }
}
